cmake_minimum_required(VERSION 3.15)

# 查找Python解释器
find_package(Python COMPONENTS Interpreter REQUIRED)

# 必须在project命令之前设置交叉编译工具链
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/arm-none-eabi-gcc.cmake)

# 启用汇编语言支持
project(Led_Blink C ASM)

# 设置编译器标志
enable_language(ASM)
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -x assembler-with-cpp")

add_definitions(
    -DN32G430
    -DUSE_STDPERIPH_DRIVER
    -DHSE_VALUE=16000000
)

# 添加RTT子目录
add_subdirectory(RTT)


include_directories(
    # 项目相关头文件
    firmware/CMSIS/core
    firmware/CMSIS/device
    User/inc
    firmware/n32g430_std_periph_driver/inc
)

# Core源文件
set(CORE_SOURCES
    User/src/bsp_led.c
    User/src/bsp_delay.c
    User/src/n32g430_it.c
    User/src/ota.c
    User/src/can_ota.c
    User/src/bsp_can.c
    User/src/log.c
    firmware/CMSIS/device/system_n32g430.c
)

# 标准库源文件
set(LIB_SOURCES
    firmware/n32g430_std_periph_driver/src/n32g430_gpio.c
    firmware/n32g430_std_periph_driver/src/n32g430_rcc.c
    firmware/n32g430_std_periph_driver/src/n32g430_flash.c
    firmware/n32g430_std_periph_driver/src/n32g430_can.c
    firmware/n32g430_std_periph_driver/src/misc.c
)

# 启动文件
set(STARTUP_SOURCES
    firmware/CMSIS/device/startup/startup_n32g430_gcc.s
)

# 合并所有源文件
set(BOOTLOADER_SOURCES
    User/src/bootloader_main.c
    ${CORE_SOURCES}
    ${LIB_SOURCES}
    ${STARTUP_SOURCES}
)

set(APP_SOURCES
    User/src/main.c
    ${CORE_SOURCES}
    ${LIB_SOURCES}
    ${STARTUP_SOURCES}
)

# 指定链接脚本
set(BOOTLOADER_LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/ldscript/bootloader.ld)
set(APP_LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/ldscript/app.ld)


# Bootloader目标
add_executable(bootloader ${BOOTLOADER_SOURCES})
target_link_libraries(bootloader 
    RTT
)
set_target_properties(bootloader PROPERTIES SUFFIX ".elf")
target_link_options(bootloader PRIVATE
    -T${BOOTLOADER_LINKER_SCRIPT}
    -Wl,-Map=${PROJECT_BINARY_DIR}/bootloader.map
    -Wl,--gc-sections,--print-memory-usage
)



# 应用程序目标
add_executable(application ${APP_SOURCES})
target_link_libraries(application 
    RTT
)
target_include_directories(application PUBLIC
    ${CMAKE_SOURCE_DIR}/User/inc
)
set_target_properties(application PROPERTIES SUFFIX ".elf")
target_link_options(application PRIVATE
    -T${APP_LINKER_SCRIPT}
    -Wl,-Map=${PROJECT_BINARY_DIR}/application.map
    -Wl,--gc-sections,--print-memory-usage
)



# 生成bin文件
add_custom_command(
    TARGET bootloader
    POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary bootloader.elf bootloader.bin
    COMMAND ${CMAKE_OBJCOPY} -O ihex bootloader.elf bootloader.hex
    COMMAND ${CMAKE_OBJDUMP} -D -S --demangle -w -z application.elf > application_full.asm
    WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
)

add_custom_command(
    TARGET application
    POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary application.elf application.bin
    COMMAND ${CMAKE_OBJCOPY} -O ihex application.elf application.hex
    COMMAND ${CMAKE_OBJDUMP} -D -S --demangle -w -z application.elf > application_full.asm
    WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
)


# 添加合并hex的命令
add_custom_target(merge_hex
    COMMAND ${CMAKE_COMMAND} -E echo "Merging hex files..."
    COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/merge_hex.py 
        ${CMAKE_BINARY_DIR}/bootloader.hex 
        ${CMAKE_BINARY_DIR}/application.hex 
        ${CMAKE_BINARY_DIR}/firmware.hex
    DEPENDS bootloader application
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    COMMENT "Merging bootloader and application hex files..."
)

# 添加一个全部构建的目标
add_custom_target(firmware ALL
    DEPENDS merge_hex
    COMMENT "Building complete firmware..."
)